package stat

import (
	"github.com/astaxie/beego/orm"
	"pq/models"
	"strconv"
)

func Run(){
	//stat_user()
}
//func stat_user(){
//	var users []orm.Params
//	slave := models.GetSlaveDb()
//	slave.Raw(`select * from pq_user where state=1`).Values(&users)
//	//stat := new(models.PqStat)
//	//master := models.GetMasterDb()
//	for _,user := range users{
//		c := Stat_user_question_count(user["id"])
//		u := ""
//		if c > 0 {
//			u = fmt.Sprintf("%.2f",(float64(Stat_user_right_answer_count(user["id"])) / float64(c)) * 100)
//		}
//		beego.Info("user_id:",user["id"])
//		beego.Warning("user_question_count:",c)
//		beego.Warning("user_right_rate:",u)
//	}
//}
func Stat_user_question_count(uid interface{},lft,rgt int) int{
	var nodes []orm.Params
	//select count('a.*') as amount from pq_cs_simu_item_connection a
	//	inner join pq_cs_simu_library b on b.id=a.pq_cs_test_simu_library_id
	//	inner join pq_test_category c on c.id = b.`pq_test_category_id`
	//	where b.pq_user_id=95 and a.is_answered = 1 and c.`lft` >= 12 and c.`rgt` <=13
	sql := `select count('a.*') as amount from pq_cs_simu_item_connection a
	inner join pq_cs_simu_library b on b.id=a.pq_cs_test_simu_library_id
    inner join pq_test_category c on c.id = b.pq_test_category_id
	where b.pq_user_id=? and a.is_answered = 1 and c.lft >= ? and c.rgt <= ?
	`
	slave := models.GetSlaveDb()
	_, _ = slave.Raw(sql, uid,lft,rgt).Values(&nodes)
	amount := nodes[0]["amount"].(string)
	a,_ := strconv.Atoi(amount)
	return a
}

func Stat_user_right_answer_count(uid interface{},lft,rgt int) int{
	var nodes []orm.Params
	sql := `select count('a.*') as amount from pq_cs_simu_item_connection a
	inner join pq_cs_simu_library b on b.id=a.pq_cs_test_simu_library_id
    inner join pq_test_category c on c.id = b.pq_test_category_id
	where b.pq_user_id=? and a.is_answered = 1 and a.is_answer_right = 1  and c.lft >= ? and c.rgt <= ?
	`
	slave := models.GetSlaveDb()
	_, _ = slave.Raw(sql, uid, lft, rgt).Values(&nodes)
	amount := nodes[0]["amount"].(string)
	a,_ := strconv.Atoi(amount)
	return a
}